//package com.rc.common.encoder;
//
//import com.fasterxml.jackson.core.JacksonException;
//import com.fasterxml.jackson.core.JsonParseException;
//import com.fasterxml.jackson.core.JsonParser;
//import com.fasterxml.jackson.databind.BeanProperty;
//import com.fasterxml.jackson.databind.DeserializationContext;
//import com.fasterxml.jackson.databind.JsonDeserializer;
//import com.fasterxml.jackson.databind.deser.ContextualDeserializer;
//import com.rc.common.constant.TypeEnum;
//import lombok.NoArgsConstructor;
//import lombok.extern.slf4j.Slf4j;
//
//import java.io.IOException;
//import java.util.Arrays;
//
//@Slf4j
//@NoArgsConstructor
//public class CodeEnumDeserializer extends JsonDeserializer<TypeEnum> implements ContextualDeserializer {
//    private Class<? extends TypeEnum> propertyClass; // 记录枚举字段的类，用于获取其定义的所有枚举值
//
//    public CodeEnumDeserializer(Class<? extends TypeEnum> propertyClass) {
//        this.propertyClass = propertyClass;
//    }
//
//    @Override
//    public TypeEnum deserialize(JsonParser p, DeserializationContext context) throws IOException, JacksonException {
//        int value;
//
//        try {
//            value = p.getIntValue();
//        } catch (JsonParseException e) {
//            return null;
//        }
//
//        return Arrays.stream(propertyClass.getEnumConstants()) // 调用Class的这个方法，获取枚举类的所有枚举值
//                .filter(e -> e.getValue() == value)
//                .findAny()
//                .orElseThrow(() -> new IllegalArgumentException("No such code of " + propertyClass.getSimpleName()));
//    }
//
//    @SuppressWarnings({"unchecked"})
//    @Override
//    public JsonDeserializer<?> createContextual(DeserializationContext context, BeanProperty property) {
//        return new CodeEnumDeserializer((Class<? extends TypeEnum>) property.getType().getRawClass()); // 获取枚举字段的类型Class
//    }
//}
